package com.ngc.mall.util;

import com.amdelamar.jhash.Hash;
import com.amdelamar.jhash.algorithms.Type;
import com.amdelamar.jhash.exception.InvalidHashException;
/**
 *  密码加解密
 *
 * @author : Chen Xiao Lin
 * @Date : 2023/4/6 14:01
 **/

public class EncryptUtil {

    /**
     * 加密密码
     *
     * @return
     */
    public static  String enrypt(String password) {
        char[] passwordChar=password.toCharArray();
        return Hash.password(passwordChar).algorithm(Type.PBKDF2_SHA256).create();
    }

    /**
     * 校验密码
     *
     * @return
     */
    public static Boolean verify(String encryptedPassword,String plainPassword) {
        char[] plainPasswordChar=plainPassword.toCharArray();
        try {
            return Hash.password(plainPasswordChar).algorithm(Type.PBKDF2_SHA256).verify(encryptedPassword);
        }catch (InvalidHashException exception){
            return false;
        }

    }
}
